package com.bite.system.manager;

import com.bit.common.redis.service.RedisService;
import com.bite.common.core.constants.CacheConstants;
import com.bite.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManager {
    @Autowired
    private RedisService redisService;

    public void addCache(Exam exam) {
        if (exam == null) return;
        String examKey = getExamKey(exam.getExamId());
        redisService.setCacheObject(examKey, exam);
        redisService.leftPushForList(getUnFinishExamListKey(), exam.getExamId());
    }

    public void deleteCache(Long examId) {
        if (examId == null) return;
        String examKey = getExamKey(examId);
        redisService.deleteObject(examKey);
        // 移除竞赛列表中的竞赛Id
        redisService.removeForList(getUnFinishExamListKey(), examId);
        // 移除题目Id
        redisService.deleteObject(getQuestionListKey(examId));
    }

    private String getExamKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getUnFinishExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    private String getQuestionListKey(Long exam) {
        return CacheConstants.EXAM_QUESTION_LIST + exam;
    }
}
